/*
 * AXP17x.h
 *
 *  Created on: 2016-6-23
 *      Author: NKJ
 */

#ifndef _AXP17X_H_
#define _AXP17X_H_

#include <stdint.h>
#include "bsp_axp17x.h"

enum SW
{
	OFF = 0,
	ON,
	IGNORE
};

enum VOFF	//关机电压
{
	VOFF2600 = 0,
	VOFF2700,
	VOFF2800,
	VOFF2900,
	VOFF3000,
	VOFF3100,
	VOFF3200,
	VOFF3300
};

enum CC		//充电电流
{
	CC100 = 0,
	CC190,
	CC280,
	CC360,
	CC450,
	CC550,
	CC630,
	CC700,
	CC780,
	CC880,
	CC960,
	CC1000,
	CC1080,
	CC1160,
	CC1240,
	CC1320
};


typedef struct _Batinfo
{
	uint8_t mark;						//bit0: 1 正在充电 	0 充电完成
										//bit1: 1 电池被放干  0 未被放干
										//bit2: 1 充电时USB被拔出过    0 充电时USB未被拔出过
										//bit3: 1 发生中断  		  0 未发生中断
										//bit4: 1 VBUS存在  		  VBUS移除
										//bit7: 1 电池一直存在	  0 电池重新插入
	uint32_t irq;						//中断寄存器状态

	uint16_t batrdc;					//mΩ 电池内阻

	uint32_t Coulomb;					//*充电库伦计数值

	uint16_t batfcc;					//*电池满充容量     	 	 mAh 经库仑计校准后此值要被改写，初次使用即为标称容量
	uint16_t batovc;					//*电池开始放电或结束充电时的电量     	 mAh
	uint16_t batrm;				    	//*电池剩余电量     	 mAh
	uint8_t  batsoc;					//电池百分比电量     	 %

	uint16_t batvolt;					//电池电压     	 mV
	uint16_t batcurrent;				//电池电流     	 mA
	int16_t  battemp;					//电池温度     	 0.1℃

	uint16_t batdischargetime;			//电池连续放电时间     s
	uint16_t batchargetime;				//电池连续充电时间     s

	uint16_t batcdt;					//电池充放电次数

}Batinfo,*pBatinfo;

extern Batinfo batinfo;

//#define AXP173
#define AXP176

//AXP173
/*
 * DCDC1	0.7~3.5V 	25mv/Step	1.2A
 * DCDC2	0.7~2.275V	25mv/Step	1.6A	VRC
 * LDO1	 					  		30mA
 * LDO2		1.8~3.3V	100mv/Step  200mA
 * LDO3		1.8~3.3V	100mv/Step  200mA
 * LDO4		0.7~3.5V	25mv/Step  	500mA
 *
 */

//AXP176
/*
 * DCDC1	0.7~3.5V 	25mv/Step	1.2A
 * LDO1						  		30mA
 * LDO2		1.8~3.3V	100mv/Step  200mA
 * LDO3		1.8~3.3V	100mv/Step  200mA
 *
 */


#define AXP17x_I2Cx_READADDRESS		0X69 //read  address  0x69
#define AXP17x_I2Cx_WRITEADDRESS    0X68 //write address  0x68

#define SWAP16(S) 					((((S) & 0xff) << 8) | (((S) >> 8) & 0xff))
#define SWAP32(L)					(((L) >> 24) |\
           	   	   	   	   	   	    (((L) & 0x00ff0000) >> 8)  |\
           	   	   	   	   	   	   	(((L) & 0x0000ff00) << 8)  |\
           	   	   	   	   	   	   	((L) << 24))
#define C2mAh(c)					(65536 * 0.5 * (c) / 3600 / ADCFREQ)

//默认电压
#define DCDC1DEFAULTV				3300
#define DCDC2DEFAULTV				2500
#define LDO2DEFAULTV				3300
#define LDO3DEFAULTV				3300
#define LDO4DEFAULTV				3300

//电池内阻
#define BATRDC						150		//mΩ 电池内阻  一般在100~200之间,不过最好根据实际测试出来的确定，
//电池容量
#define BATCAP						550		//mAh 电池额定容量  实测值

//ADC 采样率
#define ADCFREQ						25		//0X00 25HZ ,0X10 50HZ ,0X20 100HZ ,0X30 200HZ


#ifdef AXP173

#define DCDC1	0x01
#define DCDC2	0X10
#define LDO2	0X04
#define LDO3	0X08
#define LDO4	0X02


//enable LDO3 bit
#define AXP17x_DCDC1_SW				0X01 	//
#define AXP17x_DCDC2_SW				0X10 	//
#define AXP17x_LDO2_SW				0X04 	//
#define AXP17x_LDO3_SW				0X08 	//
#define AXP17x_LDO4_SW				0X02 	//


//MAX AND MIN VOLT
#define AXP17x_DCDC1MAX				3500 	//3500	mV
#define AXP17x_DCDC1MIN				700 	//700	mV

#define AXP17x_DCDC2MAX				2275 	//2275	mV
#define AXP17x_DCDC2MIN				700 	//700	mV

#define AXP17x_LDO1MAX				3300 	//3300	mV
#define AXP17x_LDO1MIN				3300 	//3300	mV

#define AXP17x_LDO2MAX				3300 	//3300	mV
#define AXP17x_LDO2MIN				1800 	//1800	mV

#define AXP17x_LDO3MAX				3300 	//3300	mV
#define AXP17x_LDO3MIN				1800 	//1800	mV

#define AXP17x_LDO4MAX				3500 	//3500	mV
#define AXP17x_LDO4MIN				700 	//700	mV



/*  AXP中断使能  */
#define	AXP17x_IRQ_USBLO				(1<< 1)	//usb 低电
#define	AXP17x_IRQ_USBRE				(1<< 2)	//usb 拔出
#define	AXP17x_IRQ_USBIN				(1<< 3)	//usb 插入
#define	AXP17x_IRQ_USBOV				(1<< 4)	//usb 过压
#define	AXP17x_IRQ_ACRE					(1<< 5)	//ac  拔出
#define	AXP17x_IRQ_ACIN					(1<< 6)	//ac  插入
#define	AXP17x_IRQ_ACOV					(1<< 7) //ac  过压

#define	AXP17x_IRQ_TEMLO				(1<< 8) //电池低温
#define	AXP17x_IRQ_TEMOV				(1<< 9) //电池过温
#define	AXP17x_IRQ_CHAOV				(1<<10) //电池充电结束
#define	AXP17x_IRQ_CHAST				(1<<11) //电池充电正在
#define	AXP17x_IRQ_BATATOU				(1<<12) //电池退出激活模式
#define	AXP17x_IRQ_BATATIN				(1<<13) //电池进入激活模式
#define AXP17x_IRQ_BATRE				(1<<14) //电池拔出
#define AXP17x_IRQ_BATIN				(1<<15) //电池插入

#define	AXP17x_IRQ_PEKSH				(1<<16) //power键长按
#define	AXP17x_IRQ_PEKLO				(1<<17) //power键短按

#define AXP17x_IRQ_LDO4LO				(1<<19) //LDO4小于设置值
#define AXP17x_IRQ_DCDC2LO				(1<<20) //DCDC2小于设置值
#define AXP17x_IRQ_DCDC1LO				(1<<21) //DCDC1小于设置值
#define AXP17x_IRQ_CHACURLO				(1<<22) //充电电流小于设置值
#define AXP17x_IRQ_ICTEMOV				(1<<23) //AXP芯片内部过温

#define AXP17x_IRQ_EXTLOWARN			(1<<24) //APS低压警告电压1

#define AXP17x_IRQ_USBSESUN				(1<<26) //USB Session End
#define AXP17x_IRQ_USBSESVA				(1<<27) //USB Session A/B
#define AXP17x_IRQ_USBUN				(1<<28) //USB 无效
#define AXP17x_IRQ_USBVA				(1<<29) //USB 有效

//#define AXP17x_IRQ_TIMER				(1<<39) //计时器超时


/*选择需要打开的中断使能*/
#define AXP17x_NOTIFIER_ON	   (AXP17x_IRQ_USBIN |\
				       			AXP17x_IRQ_USBRE |\
				       			AXP17x_IRQ_ACIN |\
				       			AXP17x_IRQ_ACRE |\
				       			AXP17x_IRQ_BATIN |\
				       			AXP17x_IRQ_BATRE |\
				       			AXP17x_IRQ_PEKLO |\
				       			AXP17x_IRQ_PEKSH |\
				       			AXP17x_IRQ_CHAST |\
				       			AXP17x_IRQ_CHAOV |\
				       			AXP17x_IRQ_DCDC1LO |\
				       			AXP17x_IRQ_DCDC2LO |\
				       			AXP17x_IRQ_LDO4LO |\
				       			AXP17x_IRQ_EXTLOWARN)


//AXP17x 电源输入状态			REG 00H
#define AXP17x_ACINORVBUSIN			(1 << 0)	//指示启动源是AC或VBUS
#define AXP17x_ACKISSVBUS			(1 << 1)	//指示 ACIN 和 VBUS 输入是否在 PCB 被短接
#define AXP17x_BATINOROUT           (1 << 2)	//0:电池在放电；1:电池被充电
#define AXP17x_VBUSOVHOLD           (1 << 3)	//指示 VBUS 接入在使用之前是否大于 VHOLD
#define AXP17x_VBUSCANUSE			(1 << 4)	//指示 VBUS 是否可用
#define AXP17x_VBUSLIVE				(1 << 5)	//0:VBUS  不存在；1:VBUS  存在
#define AXP17x_ACINCANUSE           (1 << 6)	//指示 ACIN 是否可用
#define AXP17x_ACINLIVE	            (1 << 7)	//0:ACIN  不存在；1:ACIN  存在

//AXP17x 电源工作模式以及充电状态指示	REG 01H
#define AXP17x_CHARGECURRENTEXPSTA  (1 <<10)	//0:实际充电电流等于期望电流； 1:实际充电电流小于期望电流
#define AXP17x_BATACT	            (1 <<11)	//0:未进入电池激活模式； 1:已进入电池激活模式
#define AXP17x_BATLIVE				(1 <<13)	//0:无电池连接到 AXP173； 1:电池已经连接到 AXP173
#define AXP17x_CHARGESTA            (1 <<14)	//0:未充电或充电已完成； 1:正在充电
#define AXP17x_TEMPOV	            (1 <<15)	//0:未过温； 1:过温

//数据缓存  REG 06~0BH	:只要外部电源、电池某一路电源存在，此四个字节的数据就会一直保存，不受开关机影响。默认值F0/0F/00/FF/00/00H
#define AXP17x_BUF0					0X06
#define AXP17x_BUF1					0X07
#define AXP17x_BUF2					0X08
#define AXP17x_BUF3					0X09

//REG OFFSET
#define AXP17x_SW_CTRL				0X12 //axp out put sw ctrl reg
#define AXP17x_DCDC2_VOLT_CTRL		0X23 //DCDC2(0X3F) out put volt ctrl reg
#define AXP17x_DCDC2_DYTUNREG		0X25 //DC-DC2 动态电压调节参数设置
#define AXP17x_DCDC1_VOLT_CTRL		0X26 //DCDC1(0X7F) out put volt ctrl reg
#define AXP17x_LDO23_VOLT_CTRL		0X28 //LDO2(0xf0) LDO3(0x0f) out put volt ctrl reg
#define AXP17x_LDO4_VOLT_CTRL		0X27 //LDO4(0X7F) out put volt ctrl reg
#define AXP17x_VBUS_IPSOUT_CTRL		0X30 //VBUS-IPSOUT 通路管理
#define AXP17x_VOFF_SET				0X31 //VOFF 关机电压设置
#define AXP17x_POWEROFFCTRL			0X32 //关机设置、电池检测以及 CHGLED管脚控制
#define AXP17x_CHARGECTRL1			0X33 //充电控制 1
#define AXP17x_CHARGECTRL2			0X34 //充电控制 2
#define AXP17x_PEKSET				0X36 //PEK 按键参数设置
#define AXP17x_DCDCFREQ				0X37 //DC-DC 工作频率设置
#define AXP17x_VLTFCHARGE			0X38 //电池充电低温门限设置
#define AXP17x_VHTFCHARGE			0X39 //电池充电高温门限设置
#define AXP17x_APSVWARINGSET1		0X3A //APS低电级别 1
#define AXP17x_APSVWARINGSET2		0X3B //APS低电级别 2
#define AXP17x_VLTFDISCHARGE		0X3C //电池放电低温门限设置
#define AXP17x_VHTFDISCHARGE		0X3D //电池放电高温门限设置

//中断控制 及 中断状态寄存器
#define AXP17x_IRQCTL_REG1			0X40
#define AXP17x_IRQCTL_REG2			0X41
#define AXP17x_IRQCTL_REG3			0X42
#define AXP17x_IRQCTL_REG4			0X43
#define AXP17x_IRQSTA_REG1			0X44
#define AXP17x_IRQSTA_REG2			0X45
#define AXP17x_IRQSTA_REG3			0X46
#define AXP17x_IRQSTA_REG4			0X47

#define AXP17x_IRQSTA_TIMER			0X4D //bit7 计时器超时 IRQ状态

#define AXP17x_DCDCWORKMODE			0X80 //DC-DC 工作模式选择
#define AXP17x_ADCENABLE1			0X82 //ADC 使能 1
#define AXP17x_ADCENABLE2			0X83 //ADC 使能 2
#define AXP17x_ADCFREQANDTS			0X84 //ADC 采样速率设置，TS管脚控制
#define AXP17x_TIMERCTL				0X8A //定时器控制
#define AXP17x_VBUSMONSRPCTL		0X8B //VBUS管脚监测 SRP功能控制
#define AXP17x_OVTEMPSHUTDOWN		0X8F //过温关机等功能设置

#define AXP17x_COULCTL				0XB8 //库仑计控制




#elif defined AXP176

#define DCDC1	0x01
#define LDO2	0X04
#define LDO3	0X08

//enable LDO3 bit
#define AXP17x_DCDC1_SW				0X01 	//
#define AXP17x_LDO2_SW				0X04 	//
#define AXP17x_LDO3_SW				0X08 	//


//MAX AND MIN VOLT
#define AXP17x_DCDC1MAX				3500 	//3500	mV
#define AXP17x_DCDC1MIN				700 	//700	mV

#define AXP17x_LDO1MAX				3300 	//3300	mV
#define AXP17x_LDO1MIN				3300 	//3300	mV

#define AXP17x_LDO2MAX				3300 	//3300	mV
#define AXP17x_LDO2MIN				1800 	//1800	mV

#define AXP17x_LDO3MAX				3300 	//3300	mV
#define AXP17x_LDO3MIN				1800 	//1800	mV


/*  AXP中断使能  */
#define	AXP17x_IRQ_USBLO				(1<< 1)	//usb 低电
#define	AXP17x_IRQ_USBRE				(1<< 2)	//usb 拔出
#define	AXP17x_IRQ_USBIN				(1<< 3)	//usb 插入
#define	AXP17x_IRQ_USBOV				(1<< 4)	//usb 过压

#define	AXP17x_IRQ_TEMLO				(1<< 8) //电池低温
#define	AXP17x_IRQ_TEMOV				(1<< 9) //电池过温
#define	AXP17x_IRQ_CHAOV				(1<<10) //电池充电结束
#define	AXP17x_IRQ_CHAST				(1<<11) //电池充电正在
#define	AXP17x_IRQ_BATATOU				(1<<12) //电池退出激活模式
#define	AXP17x_IRQ_BATATIN				(1<<13) //电池进入激活模式
#define AXP17x_IRQ_BATRE				(1<<14) //电池拔出
#define AXP17x_IRQ_BATIN				(1<<15) //电池插入

#define	AXP17x_IRQ_PEKSH				(1<<16) //power键长按
#define	AXP17x_IRQ_PEKLO				(1<<17) //power键短按

#define AXP17x_IRQ_DCDC1LO				(1<<21) //DCDC1小于设置值
#define AXP17x_IRQ_CHACURLO				(1<<22) //充电电流小于设置值
#define AXP17x_IRQ_ICTEMOV				(1<<23) //AXP芯片内部过温

#define AXP17x_IRQ_EXTLOWARN			(1<<24) //APS低压警告电压1

#define AXP17x_IRQ_USBUN				(1<<28) //USB 无效
#define AXP17x_IRQ_USBVA				(1<<29) //USB 有效


/*选择需要打开的中断使能*/
#define AXP17x_NOTIFIER_ON	   (AXP17x_IRQ_USBIN |\
				       			AXP17x_IRQ_USBRE |\
				       			AXP17x_IRQ_BATIN |\
				       			AXP17x_IRQ_BATRE |\
				       			AXP17x_IRQ_PEKLO |\
				       			AXP17x_IRQ_PEKSH |\
				       			AXP17x_IRQ_CHAST |\
				       			AXP17x_IRQ_CHAOV |\
				       			AXP17x_IRQ_DCDC1LO |\
				       			AXP17x_IRQ_EXTLOWARN)


//AXP17x 电源输入状态			REG 00H
#define AXP17x_ACINORVBUSIN			(1 << 0)	//0:NOT BE VBUS 1:BE VBUS
#define AXP17x_BATINOROUT           (1 << 2)	//0:电池在放电；1:电池被充电
#define AXP17x_VBUSOVHOLD           (1 << 3)	//指示 VBUS 接入在使用之前是否大于 VHOLD
#define AXP17x_VBUSCANUSE			(1 << 4)	//指示 VBUS 是否可用
#define AXP17x_VBUSLIVE				(1 << 5)	//0:VBUS  不存在；1:VBUS  存在

//AXP17x 电源工作模式以及充电状态指示	REG 01H
#define AXP17x_CHARGECURRENTEXPSTA  (1 << 10)	//0:实际充电电流等于期望电流； 1:实际充电电流小于期望电流
#define AXP17x_BATACT	            (1 << 11)	//0:未进入电池激活模式； 1:已进入电池激活模式
#define AXP17x_BATLIVE				(1 << 13)	//0:无电池连接到 AXP173； 1:电池已经连接到 AXP173
#define AXP17x_CHARGESTA            (1 << 14)	//0:未充电或充电已完成； 1:正在充电
#define AXP17x_TEMPOV	            (1 << 15)	//0:未过温； 1:过温

//数据缓存  REG 06~09H	:只要外部电源、电池某一路电源存在，此四个字节的数据就会一直保存，不受开关机影响。默认值F0/0F/00/FFH
#define AXP17x_BUF0					0X06
#define AXP17x_BUF1					0X07
#define AXP17x_BUF2					0X08	//此处存储了 电池是否是 重新插入的状态  为0 认为是重新插入
#define AXP17x_BUF3					0X09

//REG OFFSET
#define AXP17x_SW_CTRL				0X12 //axp out put sw ctrl reg
#define AXP17x_DCDC1_VOLT_CTRL		0X26 //DCDC1(0X7F) out put volt ctrl reg
#define AXP17x_LDO23_VOLT_CTRL		0X28 //LDO2(0xf0) LDO3(0x0f) out put volt ctrl reg
#define AXP17x_VBUS_IPSOUT_CTRL		0X30 //VBUS-IPSOUT 通路管理
#define AXP17x_VOFF_SET				0X31 //VOFF 关机电压设置
#define AXP17x_POWEROFFCTRL			0X32 //关机设置、电池检测以及 CHGLED管脚控制
#define AXP17x_CHARGECTRL1			0X33 //充电控制 1
#define AXP17x_CHARGECTRL2			0X34 //充电控制 2
#define AXP17x_PEKSET				0X36 //PEK 按键参数设置
#define AXP17x_APSVWARINGSET1		0X3A //APS低电级别 1
#define AXP17x_APSVWARINGSET2		0X3B //APS低电级别 2

#define AXP17x_IRQCTL_REG1			0X40
#define AXP17x_IRQCTL_REG2			0X41
#define AXP17x_IRQCTL_REG3			0X42
#define AXP17x_IRQCTL_REG4			0X43
#define AXP17x_IRQSTA_REG1			0X44
#define AXP17x_IRQSTA_REG2			0X45
#define AXP17x_IRQSTA_REG3			0X46
#define AXP17x_IRQSTA_REG4			0X47

#define AXP17x_IRQSTA_TIMER			0X4D //bit7 计时器超时 IRQ状态

#define AXP17x_ADCENABLE1			0X82 //ADC 使能 1
#define AXP17x_ADCENABLE2			0X83 //ADC 使能 2
#define AXP17x_ADCFREQANDTS			0X84 //ADC 采样速率设置，TS管脚控制
#define AXP17x_TIMERCTL				0X8A //定时器控制
#define AXP17x_OVTEMPSHUTDOWN		0X8F //过温关机等功能设置

#define AXP17x_COULCTL				0XB8 //库仑计控制

#endif



extern uint8_t bsp_InitAXP17x(void);
extern uint8_t AXP17x_SWandVoltCtrl(uint8_t way,uint8_t sw,uint16_t volt);

extern uint16_t AXP17x_GetPowerInStatus(void);
extern void AXP17x_Shutdown(void);
extern void AXP17x_CoulombSW(uint8_t sw);
extern void AXP17x_CoulombPause(void);
extern void AXP17x_CoulombClear(void);
extern uint32_t AXP17x_GetCoulombChargeCounter(void);
extern uint32_t AXP17x_GetCoulombDisChargeCounter(void);
extern void AXP17x_BatinfoMonitor(void *p);
extern uint8_t AXP17x_GetBATPercentageBaseCoul(pBatinfo batinfo);
extern uint8_t AXP17x_GetBATPercentageBaseVolt(void);

extern uint16_t AXP17x_GetVBUSVolt(void);
extern uint16_t AXP17x_GetVBUSCurrent(void);
extern uint16_t AXP17x_GetBATVolt(void);
extern uint16_t AXP17x_GetBATChargeCurrent(void);
extern uint16_t AXP17x_GetBATDisChargeCurrent(void);
extern uint16_t AXP17x_GetAPSVolt(void);
extern int16_t AXP17x_GetAXPInternalTemp(void);

extern uint16_t AXP17x_CalcAXPInternalRDC(void);

extern uint32_t AXP17x_EnableIRQ(uint32_t tmp);
extern uint32_t AXP17x_GetAndClearIRQStatus(void);
extern uint32_t AXP17x_GetIRQStatus(void);
extern uint32_t AXP17x_ClearIRQStatus(uint32_t tmp);
extern void AXP17x_IRQCallBack(uint32_t *pam);
extern void AXP17x_IRQ(void *p);

void SetShutdownVolt(uint8_t volt);
void ReadBATinfo(void);
void SaveBATinfo(void);

#endif /* AXP17x_H_ */
